package de.stromhaltig.grid;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import java.awt.Window.Type;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class DeviceDialog extends JDialog {
	private JTextField deviceid;
	private JTextField secret;
	private JTextField watt;
	private static DeviceDialog instance = null;
	public JLabel lastrequest;
	public JButton btnDecline;
	public JButton btnAccept;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DeviceDialog dialog = new DeviceDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);			
			DeviceDialog.instance = dialog;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public static DeviceDialog getInstance() {
		if(DeviceDialog.instance==null) {
			DeviceDialog dialog = new DeviceDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);		
			DeviceDialog.instance = dialog;
			dialog.deviceid.setText(Main.properties.getProperty("deviceid"));
			dialog.secret.setText(Main.properties.getProperty("secret"));
			dialog.watt.setText(Main.properties.getProperty("watt"));
			
		}
		
		return DeviceDialog.instance;
	}
	/**
	 * Create the dialog.
	 */
	public DeviceDialog() {
		setType(Type.UTILITY);
		setTitle("Grid Swarm - V0.0.0.1");
		setResizable(false);
		setBounds(100, 100, 257, 333);
		getContentPane().setLayout(new BorderLayout());
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(Color.BLACK);
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Hide");
				okButton.setBackground(Color.WHITE);
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						DeviceDialog.instance.setVisible(false);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Exit");
				cancelButton.setBackground(Color.WHITE);
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						System.exit(0);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		{
			JPanel panel = new JPanel();
			panel.setBackground(Color.WHITE);
			getContentPane().add(panel, BorderLayout.CENTER);
			panel.setLayout(new FormLayout(new ColumnSpec[] {
					ColumnSpec.decode("265px:grow"),},
				new RowSpec[] {
					RowSpec.decode("37px"),
					RowSpec.decode("94px"),
					RowSpec.decode("71px"),
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.DEFAULT_ROWSPEC,
					FormFactory.RELATED_GAP_ROWSPEC,
					RowSpec.decode("default:grow"),}));
			{
				JLabel lblNewLabel = new JLabel("");
				panel.add(lblNewLabel, "1, 1, left, top");
				lblNewLabel.setBackground(Color.WHITE);
				lblNewLabel.setIcon(new ImageIcon(DeviceDialog.class.getResource("/grid_swarm_logo.jpg")));
			}
			{
				JPanel panel_1 = new JPanel();
				panel_1.setBackground(Color.WHITE);
				panel.add(panel_1, "1, 2, fill, fill");
				panel_1.setLayout(new FormLayout(new ColumnSpec[] {
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),},
					new RowSpec[] {
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,}));
				{
					JLabel lblDeviceId = new JLabel("Device ID:");
					panel_1.add(lblDeviceId, "2, 2");
				}
				{
					deviceid = new JTextField();
					deviceid.setEnabled(false);
					panel_1.add(deviceid, "8, 2, left, default");
					deviceid.setColumns(14);
				}
				{
					JLabel lblSecret = new JLabel("Secret:");
					panel_1.add(lblSecret, "2, 4");
				}
				{
					secret = new JTextField();
					secret.setEnabled(false);
					panel_1.add(secret, "8, 4, left, default");
					secret.setColumns(14);
				}
				{
					JLabel lblWatt = new JLabel("Watt:");
					panel_1.add(lblWatt, "2, 6");
				}
				{
					watt = new JTextField();
					watt.addFocusListener(new FocusAdapter() {
						@Override
						public void focusLost(FocusEvent e) {
								Main.properties.setProperty("watt", DeviceDialog.instance.watt.getText());
								Main.saveProperties();
						}
					});
					
					panel_1.add(watt, "8, 6, left, default");
					watt.setColumns(5);
				}
			}
			{
				JPanel panel_1 = new JPanel();
				panel_1.setBackground(Color.WHITE);
				panel.add(panel_1, "1, 3, 1, 5, fill, fill");
				panel_1.setLayout(null);
				
				JLabel lblOpenRequest = new JLabel("Last Request");
				lblOpenRequest.setFont(new Font("Tahoma", Font.BOLD, 11));
				lblOpenRequest.setBounds(10, 11, 135, 14);
				panel_1.add(lblOpenRequest);
				
				btnAccept = new JButton("Accept");
				btnAccept.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						XMPPConnector.getInstance().sendMsgToMaster("OK");
						DeviceDialog.getInstance().btnAccept.setEnabled(false);
						DeviceDialog.getInstance().btnDecline.setEnabled(false);
						DeviceDialog.getInstance().lastrequest.setText("(waiting for request)");
					}
				});
				btnAccept.setEnabled(false);
				btnAccept.setBounds(54, 98, 89, 23);
				panel_1.add(btnAccept);
				
				btnDecline = new JButton("Decline");
				btnDecline.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						XMPPConnector.getInstance().sendMsgToMaster("DCL");
						DeviceDialog.getInstance().btnAccept.setEnabled(false);
						DeviceDialog.getInstance().btnDecline.setEnabled(false);
						DeviceDialog.getInstance().lastrequest.setText("(waiting for request)");
					}
				});
				btnDecline.setEnabled(false);
				btnDecline.setBounds(153, 98, 89, 23);
				panel_1.add(btnDecline);
				
				lastrequest = new JLabel("(No requests received...)");
				lastrequest.setVerticalAlignment(SwingConstants.TOP);
				lastrequest.setBounds(10, 36, 225, 51);
				panel_1.add(lastrequest);
			}
		}
	}
}
